package com.chargerlink.echarger.controller;


import com.chargerlink.echarger.pojo.Station;
import com.chargerlink.echarger.service.PoiService;
import com.chargerlink.echarger.vo.Pager;
import com.chargerlink.echarger.vo.ResponseBase;
import com.chargerlink.echarger.vo.ResponseDataVo;

import com.chargerlink.echarger.vo.request.StationVO;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by fansxnet on 17-5-5.
 */
@RestController
@RequestMapping("/portal/charge-total/rest/station")
public class StationRestController {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    PoiService poiService;

    /**
     * Double lng = 1.2; // Double | 经度
     * Double lat = 1.2; // Double | 纬度
     * Integer radius = 56; // Integer | 半径，单位米
     * Integer radiusMax = 56; // Integer | 最大扩展半径，单位米。当radius内无法达成desireCount的时候，使用这个参数进行扩展搜索。
     * Integer desireCount = 56;希望返回的最少数量，如果达不到数目，服务器会扩展半径尝试
     */

    //station/query
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public ResponseBase query(
            @RequestParam(required = true) Double lng,
            @RequestParam(required = true) Double lat,
            @RequestParam(required = true) Double radius,
            @RequestParam(required = true) Double radiusMax,
            @RequestParam(required = true) Integer desireCount) {
        try {
            List<Object> list = poiService.query(lng, lat, radius, radiusMax, desireCount);
            return  ResponseDataVo.success(list,new Pager(0,list.size(),list.size()));
        }catch (Exception e){
            logger.error(e);
            return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }


    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public ResponseBase info(
            @RequestParam(required = true) Integer id
            ) throws Exception {

        try {
            ResponseDataVo<Station> c = ResponseDataVo.success(poiService.getStation(id));
            return c;
        }catch (Exception e){
            logger.error(e);
             return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }

    @RequestMapping(value = "/parklist",method = RequestMethod.GET)
    public ResponseBase parklist(
            @RequestParam(required = true) Integer id
    ) throws Exception {

        try {
            return ResponseDataVo.success(poiService.getParklist(id));
        }catch (Exception e){
            logger.error(e);
            return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }
    @RequestMapping(value = "/chargerCorrect",method = RequestMethod.GET)
    public ResponseBase chargerCorrect(@Valid StationVO stationVO)   {
        try {
            return ResponseDataVo.success(poiService.chargerCorrect(stationVO));
        }catch (Exception e){
            logger.error(e);
            return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public ResponseBase search(
            @RequestParam(required = true) String keyword,
            /* @RequestParam(required = true) */ Double lng,
            /*@RequestParam(required = true)*/ Double lat,
            @RequestParam(required = true,defaultValue = "0") Integer offset,
            @RequestParam(required = true,defaultValue = "20") Integer limit
    ) throws Exception {

        try {
            return ResponseDataVo.success(poiService.search(keyword,lng,lat,offset,limit));
        }catch (Exception e){
            logger.error(e);
            return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }

    @RequestMapping(value = "/config",method = RequestMethod.GET)
    public ResponseBase config() throws Exception {

        try {
            return ResponseDataVo.success(poiService.config());
        }catch (Exception e){
            logger.error(e);
            return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }

    @RequestMapping(value = "/stakeRepair",method = RequestMethod.POST)
    public ResponseBase stakeRepair(
            @RequestParam(required = true) String stationId,
            @RequestParam(required = true) String stakeNo,
            @RequestParam(required = true) String repairType
    ) throws Exception {
        try {
            return ResponseDataVo.success(poiService.stakeRepair(stationId,stakeNo,repairType));
        }catch (Exception e){
            logger.error(e);
            return ResponseDataVo.error(e.getLocalizedMessage());
        }

    }

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        return "ok";
    }
}
